package com.ghxxzx.daglcustom.business_form.web;

import com.ghxxzx.daglcustom.business_form.domain.FormPo;
import com.ghxxzx.daglcustom.business_form.domain.FormValsPo;
import com.ghxxzx.daglcustom.business_form.domain.dto.FormJsonDto;
import com.ghxxzx.daglcustom.business_form.service.Form2Service;
import com.ghxxzx.daglcustom.business_form.service.FormValsService;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tmsps.ne4spring.base.BaseAction;
import org.tmsps.ne4spring.utils.ChkUtil;
import org.tmsps.ne4spring.utils.JsonUtil;
import org.tmsps.ne4spring.utils.wrapper.WrapMapper;
import org.tmsps.ne4spring.utils.wrapper.Wrapper;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.text.ParseException;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/form/json")
public class FormJsonAction extends BaseAction {

	@Resource
	private TaskService taskService;
	@Autowired
	private Form2Service formService;
	@Autowired
	private FormValsService formValsService;

	@GetMapping("/init_form")
	public Wrapper<String> init_form(String kid) throws ParseException {
		FormPo formPo = bs.findById(kid, FormPo.class);
		if (ChkUtil.isNull(formPo.getWidget_json())) {
			formPo.setWidget_json(formPo.getJson());
			bs.updateObj(formPo);
		}
		String jsonStr = formPo.getWidget_json();
		JSONObject json = JsonUtil.jsonStrToJsonObject(jsonStr);
		if (json == null) {
			json = new JSONObject();
		}
		return WrapMapper.ok(json.toJSONString());
	}

	@PostMapping("/save")
	public Wrapper<String> save(@Valid @RequestBody FormJsonDto formJsonDto) throws ParseException {
		FormPo formPo = bs.findById(formJsonDto.getKid(), FormPo.class);
		formPo.setJson(formJsonDto.getJson());
		formPo.setWidget_json(formJsonDto.getWidget_json());
		bs.updateObj(formPo);
		return WrapMapper.ok("表单设置成功");
	}

	@GetMapping("/get_json")
	public Wrapper<JSONObject> get_json(String task_id) {
		Task task = taskService.createTaskQuery().taskId(task_id).singleResult();
		// 1. 获取表单参数
		String form_key = task.getFormKey();
		FormPo formPo = formService.getFormByFormKey(form_key);
		// 2.获取上次表单提交数据
		Map<String, Object> variables = taskService.getVariables(task.getId());
		System.err.println(variables);
		// 3.组合
		JSONObject result = new JSONObject();
		result.put("form", formPo);
		result.put("values", variables);
		return WrapMapper.ok(result);
	}

	@GetMapping("/get_json_new")
	public Wrapper<JSONObject> get_json_new(String task_id) {
		Task task = taskService.createTaskQuery().taskId(task_id).singleResult();
		// 1. 获取表单参数
		String form_key = task.getFormKey();
		FormPo formPo = formService.getFormByFormKey(form_key);

		String values = "{}";
		// 2.1 加载草稿
		FormValsPo formValsPo = formValsService.getFormValByTaskId(task_id);

		// 2.2 获取上次表单提交数据 -->改为-->如果是被打回来的表单,加载上一次添加的值,用于修改
		if (formValsPo == null) {
			formValsPo = formValsService.getPreFormVals(task.getProcessInstanceId(), form_key);
		}
		if (formValsPo != null) {
			values = formValsPo.getForm_vals_json();
		}

		// 3.组合
		JSONObject result = new JSONObject();
		result.put("form", formPo);
		result.put("values", values);
		return WrapMapper.ok(result);
	}

	@GetMapping("/get_json_formkey")
	public Wrapper<JSONObject> get_json_formkey(String form_key) {
		FormPo formPo = formService.getFormByFormKey(form_key);
		// 2.获取上次表单提交数据
		Map<String, Object> variables = new HashMap<>();

		// 3.组合
		JSONObject result = new JSONObject();
		result.put("form", formPo);
		result.put("values", variables);
		return WrapMapper.ok(result);
	}

}
